package com.zxy.db4;

import java.sql.Connection;
import java.sql.SQLException;
import com.zxy.common.Com_Para;

public class DBManager4
{
	private static PooledConnection4	conn;
	public static ConnectionPool4		connectionPool;
	public static DBManager4			inst;
	public static boolean bUse					= false;

	public void close()
	{
		try
		{
			connectionPool.closeConnectionPool();
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
	}

	public static void closeConnectionPoolTimeOut()
	{
		try
		{
			connectionPool.closeConnectionPoolTimeOut();
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
	}

	@SuppressWarnings("static-access")
	public DBManager4()
	{
		if(inst != null)
			return;
		com.zxy.model.T_DB_CONFIG tdc = (com.zxy.model.T_DB_CONFIG) Com_Para.getInstance().getInstance().htDb.get("4");
		String connStr = tdc.getDB_url();
		if(tdc.getDB_DriverClassName().equals("org.sqlite.JDBC"))
		{
			connStr = connStr.replaceAll("@",Com_Para.ApplicationPath
					+ Com_Para.zxyPath);
		}
		connectionPool = new ConnectionPool4(tdc.getDB_DriverClassName(),connStr,tdc.getDB_username(),tdc.getDB_password());
		try
		{
			connectionPool.createPool();
			inst = this;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	public static PooledConnection4 getConnection()
	{
		if(inst == null)
		{
			new DBManager4();
		}
		try
		{
			conn = connectionPool.getConnection();
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
		return conn;
	}

	public static void returnConnection(Connection conn)
	{
		connectionPool.returnConnection(conn);
	}
}